<template>
	<view class="password-container">

		<!-- 表单 -->
		<view class="form-section">
			<view class="form-item">
				<text class="form-label">原密码</text>
				<view class="form-input">
					<input 
						type="password" 
						v-model="formData.oldPassword" 
						placeholder="请输入原密码" 
						password
					/>
				</view>
			</view>
			
			<view class="form-item">
				<text class="form-label">新密码</text>
				<view class="form-input">
					<input 
						type="password" 
						v-model="formData.newPassword" 
						placeholder="请输入新密码" 
						password
					/>
				</view>
			</view>
			
			<view class="form-item">
				<text class="form-label">确认密码</text>
				<view class="form-input">
					<input 
						type="password" 
						v-model="formData.confirmPassword" 
						placeholder="请再次输入新密码" 
						password
					/>
				</view>
			</view>
		</view>
		
		<!-- 提示 -->
		<view class="tip-box">
			<text class="tip-text">· 密码长度应为6-16位</text>
			<text class="tip-text">· 建议使用字母、数字和符号的组合</text>
		</view>
		
		<!-- 提交按钮 -->
		<button class="save-btn" @click="changePassword">确认修改</button>
	</view>
</template>

<script>
import { changePassword } from '@/api/user.js';

export default {
	data() {
		return {
			formData: {
				oldPassword: '',
				newPassword: '',
				confirmPassword: ''
			}
		}
	},
	methods: {
		// 返回上一页
		goBack() {
			uni.navigateBack();
		},
		
		// 修改密码
		async changePassword() {
			// 表单验证
			if (!this.formData.oldPassword) {
				uni.showToast({
					title: '请输入原密码',
					icon: 'none'
				});
				return;
			}
			
			if (!this.formData.newPassword) {
				uni.showToast({
					title: '请输入新密码',
					icon: 'none'
				});
				return;
			}
			
			if (this.formData.newPassword.length < 6 || this.formData.newPassword.length > 16) {
				uni.showToast({
					title: '新密码长度应为6-16位',
					icon: 'none'
				});
				return;
			}
			
			if (this.formData.newPassword !== this.formData.confirmPassword) {
				uni.showToast({
					title: '两次输入的密码不一致',
					icon: 'none'
				});
				return;
			}
			
			uni.showLoading({
				title: '提交中...'
			});
			
			try {
				const res = await changePassword({
					oldPassword: this.formData.oldPassword,
					newPassword: this.formData.newPassword
				});
				
				console.log(res);
				
				if (res.code == 200) {
					uni.showToast({
						title: '密码修改成功',
						icon: 'success'
					});
					
					// 清空表单
					this.formData = {
						oldPassword: '',
						newPassword: '',
						confirmPassword: ''
					};
					
					// 延迟返回
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				} else if (res.code == 201) {
					// 密码修改成功，需要重新登录
					uni.showToast({
						title: res.message || '密码修改成功，请重新登录',
						icon: 'success'
					});
					
					// 清空登录信息
					setTimeout(() => {
						uni.removeStorageSync('token');
						uni.removeStorageSync('userInfo');
						
						// 跳转到登录页
						uni.reLaunch({
							url: '/pages/login/login'
						});
					}, 1500);
				} else {
					uni.showToast({
						title: res.message || '修改失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('修改密码失败', error);
				uni.showToast({
					title: '修改失败，请稍后重试',
					icon: 'none'
				});
			} finally {
				uni.hideLoading();
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.password-container {
	min-height: 100vh;
	background-color: #f8f8f8;
	padding-bottom: 40rpx;
}

.nav-bar {
	display: flex;
	align-items: center;
	height: 90rpx;
	background-color: #fff;
	padding: 0 30rpx;
	position: relative;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	background: linear-gradient(to bottom, #2979ff, #5e9fff);
}

.back-btn {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 10;
}

.nav-title {
	position: absolute;
	left: 0;
	right: 0;
	text-align: center;
	font-size: 34rpx;
	font-weight: 500;
	color: #333;
}

.form-section {
	background-color: #fff;
	margin-top: 20rpx;
}

.form-item {
	display: flex;
	align-items: center;
	padding: 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.form-item:last-child {
	border-bottom: none;
}

.form-label {
	width: 180rpx;
	font-size: 30rpx;
	color: #333;
}

.form-input {
	flex: 1;
}

.form-input input {
	height: 60rpx;
	font-size: 30rpx;
	color: #333;
	width: 100%;
}

.tip-box {
	padding: 30rpx;
}

.tip-text {
	font-size: 26rpx;
	color: #999;
	line-height: 1.8;
	display: block;
}

.save-btn {
	width: 90%;
	height: 90rpx;
	line-height: 90rpx;
	background: linear-gradient(to right, #007AFF, #1CB5E0);
	color: #fff;
	font-size: 32rpx;
	border-radius: 45rpx;
	margin-top: 60rpx;
	box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
}

.save-btn:active {
	transform: scale(0.98);
	box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
}
</style> 